<?php

namespace app\admin\model;

use think\Model;
use think\Request;

class AdminLog extends Model {
    //

    /**
     * 新增日志
     * @param type $id
     * @param type $username
     * @param type $type
     * @param type $title
     * @param type $content
     * @return type
     */
    public function log($id, $username, $type, $title, $content) {
        $data = [];
        $request = Request::instance();
        $data['admin_id'] = $id;
        $data['username'] = $username;
        $data['type'] = $type;
        $data['url'] = $request->pathinfo();
        $data['title'] = $title;
        $data['content'] = $content;
        $data['ip'] = $request->ip();
        $data['useragent'] = Request::instance()->header('user-agent');
        $data['created_at'] = time();
        return $this->insert($data);
    }

    /**
     * 登录日志
     * @param type $id
     * @param type $username
     * @return type
     */
    public function loginLog($id, $username, $content = '') {
        $title = '登录';
        $content = json_encode(Request::instance()->param(false)) . '[' . $content . ']';
        return $this->log($id, $username, 1, $title, $content);
    }

}
